#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
#include<vector>

using namespace std;

//移动零
class Solution {
public:
    void moveZeroes(vector<int>& nums)
    {
        int cur = 0;
        int dest = -1;
        for (cur = 0; cur < nums.size(); cur++)
        {
            if (nums[cur] != 0)
            {
                swap(nums[++dest], nums[cur]);
            }
        }
    }
};

//复写零，第一次
class Solution {
public:
    void duplicateZeros(vector<int>& arr)
    {
        int cur = 0;
        int dest = -1;
        while (dest < arr.size())
        {
            if (arr[cur] == 0)
            {
                dest += 2;
            }
            else {
                dest += 1;
            }
            cur++;
        }
        if (arr[cur] == 0)
        {
            arr[dest - 1] = 0;
            cur--;
            dest -= 2;
        }
        while (cur >= 0)
        {
            if (arr[cur] == 0)
            {
                arr[dest--] = 0;
                arr[dest--] = 0;
            }
            else {
                arr[dest--] = arr[cur];
            }
            cur--;
        }

    }
};

//复写零更正，问题在于第一步确定好dest与cur的位置，如果直接用dest比较，就会晚一步比较，以及第二步，dest越界的情况不一定是cur正好指在0的位置，被自己举得例子迷惑了
class Solution {
public:
    void duplicateZeros(vector<int>& arr)
    {
        int cur = 0;
        int dest = -1;
        while (cur < arr.size())
        {
            if (arr[cur] == 0)
            {
                dest += 2;
            }
            else {
                dest += 1;
            }
            if (dest >= arr.size() - 1)
            {
                break;
            }
            cur++;
        }
        if (dest == arr.size())
        {
            arr[dest - 1] = 0;
            cur--;
            dest -= 2;
        }
        while (cur >= 0)
        {
            if (arr[cur] == 0)
            {

                arr[dest--] = 0;
                arr[dest--] = 0;
                cur--;

            }
            else {
                arr[dest--] = arr[cur];
                cur--;
            }

        }

    }
};


int main()
{


	return 0;
}